﻿using BankCreditPortfolio.DAL.Entities;
using System.Linq;

namespace BankCreditPortfolio.DAL.Services
{
    public class CurrencyService : DataService<Currency>
    {
        public Currency GetCurrencyByTitle(string title)
        {
            var query = this.CreateQuery().Where(x => x.Title == title);
            return query.Count() > 0 ? query.First() : null;
        }

        public override bool Delete(Currency entity)
        {
             return base.Delete(base.Find(entity.CurrencyId));
        }

        public bool IsUsed(Currency currency)
        {
            var tariffFlag =
                (new TariffService()).GetAll().Where(x => x.Currency.CurrencyId == currency.CurrencyId).Count() >= 0;
            var orderInfoFlag =
                (new OrderInfoService()).GetAll().Where(
                    x =>
                    x.Currency.CurrencyId == currency.CurrencyId || x.GainsCurrency.CurrencyId == currency.CurrencyId).
                    Count() >= 0;
            var guarantorFlag =
                (new GuarantorService()).GetAll().Where(x => x.GainsCurrency.CurrencyId == currency.CurrencyId).Count() >=
                0;
            var depositFlag =
                (new DepositService()).GetAll().Where(x => x.Currency.CurrencyId == currency.CurrencyId).Count() >= 0;
            var paymentFlag =
                (new PaymentService()).GetAll().Where(x => x.Currency.CurrencyId == currency.CurrencyId).Count() >= 0;
            return tariffFlag || orderInfoFlag || guarantorFlag || depositFlag || paymentFlag;
        }
    }
}